package com.geoway.adf.report.jpa;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.UUID;

/**
 * @Author: lzw
 * @Date: 2024/1/17 9:50
 * @Description: 自定义主键生成器 无下划线风格UUID
 *               更多策略参见DefaultIdentifierGeneratorFactory
 */
public class SimpleUUIDGenerator implements IdentifierGenerator {
    public static String simpleUUID(){
        return UUID.randomUUID().toString().replace("-", "");
    }
    @Override
    public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
        Serializable id = session.getEntityPersister(null, object).getClassMetadata().getIdentifier(object, session);
        return id != null && StringUtils.hasText(id.toString())?id:simpleUUID();
    }
}